#include "mynot_translation.h"
#include<qdebug.h>
#include<qfile.h>
#include<string.h>

mynot_translation::mynot_translation(QObject *parent) : QObject(parent)
{


}

void mynot_translation::sql_engchi(QString engfile,QString chifile )
{
    if(engfile.right(4)==".srt"||engfile.right(4)==".doc"||engfile.right(4)==".txt")
    {
        chunru_srt(engfile,chifile);
        return;
    }
    else if(engfile.right(4)==".lrc")
    {
        chunru_lrc(engfile,chifile);
        return;
    }
    chunru_srt(engfile,chifile);
}

void mynot_translation::chunru_srt(QString engfile, QString chifile)
{
    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_engchi.db");

    //打开数据库
    if(mysql.open()==true)
    {
        QSqlQuery query(mysql);//不可以做成员,会报错
        query.exec("DROP TABLE english");
        query.exec("DROP TABLE chinese");
        //创建表

        query.exec("create table english(id int primary key, time double,eng varchar(600))");
        query.exec("create table chinese(id int primary key, time double,chi varchar(600))");
        //字幕文件
        QFile txt(engfile);
        QFile txt_chinese(chifile);
        if(txt.open(QIODevice::ReadOnly)&&txt_chinese.open(QIODevice::ReadOnly))
        {
            QByteArray arr[4];
            QByteArray arr_chinese[4];
            while(txt.atEnd()==false)
            {

                for(int i=0;i<4;i++)
                {
                    QString streng=txt.readLine();
                    streng=streng.toUtf8();
                    arr[i]=streng.toUtf8();

                    QString strchi=txt_chinese.readLine();
                    arr_chinese[i]=strchi.toUtf8();
                }

                //时间转换
                double time=this->totime(arr[1]);
                //
                QString eng=arr[2];
                eng=eng.replace(QRegExp("'"),QString("''"));

                QSqlQuery query(mysql);//不可以做成员,会报错
                //插入数据
                query.exec(QString("insert into english(id,time,eng) values(%1,%2,'%3')").arg(arr[0].data()).arg(time).arg(eng) );
                query.exec(QString("insert into chinese(id,time,chi) values(%1,%2,'%3')").arg(arr[0].data()).arg(time).arg(arr_chinese[2].data()) );

            }

            txt.close();
            txt_chinese.close();

        }

        //关闭
        mysql.close();
    }
    else
    {
        qDebug()<<"打开数据库失败";
    }

    return;
}

void mynot_translation::chunru_lrc(QString engfile, QString chifile)
{
    qDebug()<<"mynot_translation::sql_engchi()";
    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_engchi.db");

    //打开数据库
    if(mysql.open()==true)
    {
        QSqlQuery query(mysql);//不可以做成员,会报错
        query.exec("DROP TABLE english");
        query.exec("DROP TABLE chinese");
        //创建表
        query.exec("create table english(id int primary key, time double,eng varchar(600))");
        query.exec("create table chinese(id int primary key, time double,chi varchar(600))");
        //字幕文件
        QFile txt(engfile);
        QFile txt_chinese(chifile);


        if(txt.open(QIODevice::ReadOnly)&&txt_chinese.open(QIODevice::ReadOnly))
        {
            QByteArray eng;
            QByteArray chinese;


            int chishu=1;
            while(txt.atEnd()==false)
            {
                QTextCodec *codec = QTextCodec::codecForName("GBK");
                QString arr=codec->toUnicode(txt.readLine());
                chinese=txt_chinese.readLine();


                if(isnum(arr.mid(1,1) )==false)
                    continue;

                //arr = QTextCodec::codecForName( "GBK" )->toUnicode(eng);

                if(arr=="\n")
                    continue;

                if(arr=="\r\n")
                    continue;

                //时间转换
                double time=this->totime_lrc(arr);
                //
                arr=arr.replace(QRegExp("'"),QString("''"));

                QSqlQuery query(mysql);//不可以做成员,会报错
                //插入数据
                query.exec(QString("insert into english(id,time,eng) values(%1,%2,'%3')").arg(chishu).arg(time).arg(arr.mid(10)) );
                query.exec(QString("insert into chinese(id,time,chi) values(%1,%2,'%3')").arg(chishu).arg(time).arg(QString(chinese).mid(10)));

                chishu++;

            }

            txt.close();
            txt_chinese.close();

        }

        //关闭
        mysql.close();
    }
    else
    {
        qDebug()<<"打开数据库失败";
    }

    return;
}

bool mynot_translation::isnum(QString str)
{
    if(str>="0"&&str<="9")
        return true;

    return false;
}

double mynot_translation::totime_lrc(QString arrtime)
{
    //[00:10.50]annoy vt. 使烦恼；打扰
    double time_3,time_2,time_2_2,time_1,time_1_2 = 0.0;

    time_3=arrtime.mid(7,1).toDouble()/10;

    time_2=arrtime.mid(5,1).toDouble();
    time_2_2=arrtime.mid(4,1).toDouble()*10;

    time_1=arrtime.mid(2,1).toDouble();
    time_1_2=arrtime.mid(1,1).toDouble()*10;

    return (time_1+time_1_2)*60+time_2+time_2_2+time_3;

}

void mynot_translation::chunru_shuanru(QString filename)
{
    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_engchi.db");

    //打开数据库
    if(mysql.open()==true)
    {
        QSqlQuery query(mysql);//不可以做成员,会报错
        query.exec("DROP TABLE english");
        query.exec("DROP TABLE chinese");
        //创建表
        query.exec("create table english(id int primary key, time double,eng varchar(600))");
        query.exec("create table chinese(id int primary key, time double,chi varchar(600))");
        //字幕文件
        QFile txt(filename);
        if(txt.open(QIODevice::ReadOnly))
        {
            QString str[5];

            while(txt.atEnd()==false)
            {
                for(int i=0;i<5;i++)
                    str[i]=txt.readLine();

                //时间转换
                double time=this->totime(str[1].toUtf8());

                str[2]=str[2].replace(QRegExp("'"),QString("''"));

                QSqlQuery query(mysql);//不可以做成员,会报错
                //插入数据
                query.exec(QString("insert into english(id,time,eng) values(%1,%2,'%3')").arg(str[0]).arg(time).arg(str[2]) );
                query.exec(QString("insert into chinese(id,time,chi) values(%1,%2,'%3')").arg(str[0]).arg(time).arg(str[3]) );

            }
        txt.close();
        }
        mysql.close();
    }
}


bool mynot_translation::isGBK(unsigned char *data, int len)
{

   int i  = 0;
   while (i < len)  {
       if (data[i] <= 0x7f) {
           //编码小于等于127,只有一个字节的编码，兼容ASCII
           i++;
           continue;
       } else {
           //大于127的使用双字节编码
           if  (data[i] >= 0x81 &&
               data[i] <= 0xfe &&
               data[i + 1] >= 0x40 &&
               data[i + 1] <= 0xfe &&
               data[i + 1] != 0xf7) {
               i += 2;
               continue;
           } else {
               return false;
           }
       }
   }
   return true;


}

void mynot_translation::shanchubiao()
{
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
       mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_engchi.db");

    if(mysql.open()==true)
    {
        QSqlQuery query;
        query.exec("DROP TABLE english");
        query.exec("DROP TABLE chinese");

        //关闭
        mysql.close();
    }
    else
    {
        qDebug()<<"打开数据库失败";
    }



}

double mynot_translation::totime(QByteArray arrtime)
{
    QString buf_time(arrtime);
    char* p=buf_time.toUtf8().data();
    double time=0;

    double time_3;
//3
    for(int i=0;i<=9;i++)
    {
        if(p[9]==QString("%1").arg(i))
            time_3=0.1*i;

    }
//2
    double time_2[2];
    for(int i=0;i<=9;i++)
    {
        if(p[6]==QString("%1").arg(i))
            time_2[0]=10*i;

    }
    for(int i=0;i<=9;i++)
    {
        if(p[7]==QString("%1").arg(i))
            time_2[1]=i;

    }
    //1
    double time_1[2];
    for(int i=0;i<=9;i++)
    {
        if(p[3]==QString("%1").arg(i))
            time_1[0]=10*i*60;

    }
    for(int i=0;i<=9;i++)
    {
        if(p[4]==QString("%1").arg(i))
            time_1[1]=i*60;

    }

    time=time_3+time_2[0]+time_2[1]+time_1[0]+time_1[1];

    delete [] p;
    return time;
}
